// pages/service/service.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    state:3,
    show: false,
    checked: false,
    checked1: false,
    checked2: false,
    checked3: false,
    createArr:[],
    actions: [
      { value:0,name: '不限',},{value:1,name: '待审核',},{value:2,name: '已取消',},{value:3,name: '待评价',},{value:4,name: '已完成',}
    ],
    listAll:[
      // {id:1,orderid:32489565655656,state:1,tag:1,address:'广州市番禺区石岗路地址地址地址地址地址',user:'刘德华',active:1,time:'2020/09/06'},
      // {id:2,orderid:1212012232323,state:2,tag:2,address:'广州番从化区某路口',user:'白某',active:2,time:'2020/09/23'},
      // {id:3,orderid:1212012232323,state:3,tag:2,address:'广州番从化区某路口',user:'白某',active:3,time:'2020/09/23'},
    ],
    index: 0,
    myl: false,
    x_stage:0,
    page_token: 0,
  },
  
  onClose() {this.setData({ show: false });}, //关闭筛选
  handbtnstatus(){this.setData({ show: true });},//弹起上拉菜单，筛选
  onSelect(event) { //全部订单筛选
    var that = this;
    let fuilterId = event.currentTarget.dataset.value; //筛选选中
    that.setData({ x_stage : fuilterId })
    that.setData({ page_token : 0 }) //重置页码
    let index = that.data.index;//订单类型
    let data = { page_size: 10,page_token:0,oStage:that.data.x_stage,ordertype:index  } //订单类型默认为0及所有
    that.api(data);
    this.setData({ show: false });
  },
  //顶级分类
  scroll(e){
    var that = this;
    let index = e.detail.index;
    this.setData({ page_token : 0 }) //页码
    this.setData({ x_stage : 0 }) //订单状态 0为全部
    this.setData({ index:index }) //订单类型 0为全部
    let data = {  ordertype: index, page_size: 10, oStage:that.data.x_stage, page_token: 0};
    that.api(data)
    console.log(data)
  },
  api(data) {
    var that = this;
    if(data.oStage === 0) delete data.oStage
    getApp().ports.api19({data}).then((res) => {
      console.log(res.data); //全部预约
      let datalist = res.data;
      // that.marketInfo(datalist);
      if (data.page_token >= 1) {
        if (that.data.Loading) {
          wx.hideLoading({
            success: (res) => {},
          })
        }
        datalist.results.forEach(item => {
         item.time = item.create_time.substring(0, 10);
          that.data.listAll.results.push(item)
          if(item.type == 4){
            item.discountHouseTitle = '增值签约'
            item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
          }
          if(item.type == 2 || item.type == 3){
            if (item.banner) {
              item.banner = that.data.imageRoot + item.banner;
            }else{
              item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
            }
          }
        })
        datalist.results = that.data.listAll.results
      }else{
        datalist.results.forEach(item => {
          item.time = item.createTime.substring(0, 10);
          if(item.type == 4){
            item.discountHouseTitle = '增值签约'
            item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
          }
          if(item.type == 2 || item.type == 3){
            if (item.banner) {
              item.banner = that.data.imageRoot + item.banner;
            }else{
              item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
            }
          }
         })
      }
      that.setData({ listAll: datalist,myl: false  })
    }).catch(() => {
      console.error('全部签约订单错了？');
    });
  },
marketInfo(datalist){
  let that = this;
  //获取用户信息
  let liky = datalist.results.filter((p)=>{ return p.marketer })
  let v = [];
  for(let i =0;i<liky.length;i++){  v[i] = liky[i].marketer }
  // console.log(v)
  getApp().ports.api38({data:v}).then((res)=>{
    // console.log(res.data)
    for(let i in datalist.results){
      for(let j in res.data){
        if(datalist.results[i].marketer == res.data[j].id){
          datalist.results[i].img = res.data[j].avatar_url
          datalist.results[i].markName = res.data[j].nickname
          datalist.results[i].telphone = res.data[j].phone
        }
      }
    }
    that.setData({ listAll: datalist })
    // console.log(datalist)
  }).catch(()=>{console.error('批量获取用户电话号码！')})
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.orderAll();
    wx.hideShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
    let data = { ordertype: 0, oStage:0, page_size: 10, page_token: this.data.page_token }
    this.api(data);
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: app.globalData.color,
    })
    if(options.scene){ //扫码进入
      let parms = { key : options.scene }
      app.ports.apiqrdata({parms}).then(res => {
        // console.log(JSON.parse(res.data))
        let datalist = JSON.parse(res.data)
        let invitestId  = datalist.userid //邀请人的id
        let sharehouseId = options.sharehouseId //房源id
        let type = options.type //订单类型
        let housetype = options.housetype //房源类型
        if(housetype == 1){
          var datas = {foreclosedHouseId: sharehouseId }
        }else if(housetype ==2){
          var datas = {discountHouseId: sharehouseId }
        }
        let data = { user_pk  : invitestId }
        // console.log(data)
        app.ports.apiinvites({data}).then(res=>{ //绑定邀请
          console.log('邀请绑定成功！')
        }).catch(()=>{console.error('建立邀请关系失败！')})
        if(type == 1){ //1优选
          let data = datas
          app.ports.fpljqy({data}).then(res=>{
            console.log('发起签约成功！')
          }).catch(()=>{console.error('添加订单失败!')})
        }else if(type == 2){ //2折扣
          let data = datas
          app.ports.zkljqy({data}).then(res=>{
            console.log('发起签约成功！')
          }).catch(()=>{console.error('添加订单失败!')})
        }else if(type == 3){ //3放盘
          // let data = datas
          app.ports.cjfp({}).then(res=>{
            console.log('发起签约成功！')
          }).catch(()=>{console.error('添加订单失败!')})
        }
      }).catch(()=>{console.error('拉取启动参数！')})
    }
    if(options.userid){ //分享进入页面,传过来的id作为判定依据
      let sharehouseId = options.sharehouseId
      let type = options.type
      let userid = options.userid
      let housetype = options.housetype
      // if(!shareId) return false
      //share=3b7cd9ce-ab20-490d-8f6c-2fe31550e94c
      let data = { user_pk  : userid }
      app.ports.apiinvites({data}).then(res=>{ //绑定邀请
        console.log('邀请绑定成功！')
      }).catch(()=>{console.error('建立邀请关系失败！')})
      if(housetype == 1){
        var datas = {foreclosedHouseId: sharehouseId }
      }else if(housetype ==2){
        var datas = {discountHouseId: sharehouseId }
      }
      if(type == 1){ //1优选
        let data = datas
        app.ports.fpljqy({data}).then(res=>{
          console.log('发起签约成功！')
        }).catch(()=>{console.error('添加订单失败!')})
      }else if(type == 2){ //2折扣
        let data = datas
        app.ports.zkljqy({data}).then(res=>{
          console.log('发起签约成功！')
        }).catch(()=>{console.error('添加订单失败!')})
      }else if(type == 3){ //3放盘
        // let data = datas
        app.ports.cjfp({}).then(res=>{
          console.log('发起签约成功！')
        }).catch(()=>{console.error('添加订单失败!')})
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.btn = this.selectComponent("#cellbox");
    // this.btn1 = this.selectComponent("#cellbox1");
    // this.btn2 = this.selectComponent("#cellbox2");
    // this.btn3 = this.selectComponent("#cellbox3");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },
  onReachBottom() { //上拉加载更多
    var that = this;
    //检查是否还有数据可以加载
    var total_size = that.data.listAll.total; //数据总数
    var size = that.data.listAll.results.length;  //当前数组长度，及数据
    var page_token = that.data.page_token += 1; //每次下拉，页码加1
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        ordertype:that.data.index,
        page_token: page_token,
        oStage:that.data.x_stage,
        page_size: 10
      }
      that.data.Loading = wx.showLoading({ title: '加载中',  })
      that.api(data)
      // console.log(size, data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({ myl: true })
    }
    that.setData({
      Loading: that.data.Loading,
      page_token:page_token
    })
  },
  
})

// all(){ //全选催促
//   console.log(this.btn.data.result)
// },
// onChange(event) {
//   // console.log(this.btn)
//   // console.log(event.detail)
//   this.setData({checked: event.detail,});
// },
// onChange1(event) {
//   this.setData({checked1: event.detail,});
// },
// onChange2(event) {
//   this.setData({checked2: event.detail,});
// },
// onChange3(event) {
//   this.setData({checked3: event.detail,});
// },
// botton(){
//   if(this.data.checked==true){
//     this.btn.queryall();
//   }else{
//     this.btn.del();
//   };
//   if(this.data.checked1==true){
//     this.btn1.queryall();
//   }else{
//     this.btn1.del();
//   };
//   if(this.data.checked2==true){
//     this.btn2.queryall();
//   }else{
//     this.btn2.del();
//   };
//   if(this.data.checked3==true){
//     this.btn3.queryall();
//   }else{
//     this.btn3.del();
//   }
// },

//接受数据过来的数据
// getAddInfo(e){
//   // console.log(e.detail)
//   if(e.detail.length<this.data.listAll.length){
//     this.setData({ checked: false });
//   }else if(e.detail.length=this.data.listAll.length){
//     this.setData({ checked: true });
//   }
// },
// getAddInfo1(e){
//   // console.log(e.detail)
//   if(e.detail.length<this.data.listZq.length){
//     this.setData({ checked1: false });
//   }else if(e.detail.length=this.data.listZq.length){
//     this.setData({ checked1: true });
//   }
// },
// getAddInfo2(e){
//   // console.log(e.detail)
//   if(e.detail.length<this.data.listFp.length){
//     this.setData({ checked2: false });
//   }else if(e.detail.length=this.data.listFp.length){
//     this.setData({ checked2: true });
//   }
// },
// getAddInfo3(e){
//   // console.log(e.detail)
//   if(e.detail.length<this.data.listPm.length){
//     this.setData({ checked3: false });
//   }else if(e.detail.length=this.data.listPm.length){
//     this.setData({ checked3: true });
//   }
// },